package ocean.chinacraft.common;

import net.minecraft.block.*;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.potion.Effects;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import ocean.chinacraft.common.world.block.decoration.LanternFestivalLanterns;
import ocean.chinacraft.common.world.block.decoration.Window;
import ocean.chinacraft.common.world.block.function.FurnacePlatformBlock;
import ocean.chinacraft.common.world.block.function.JadeWorkbenchBlock;
import ocean.chinacraft.common.world.block.function.SericultureFrameBlock;
import ocean.chinacraft.common.world.block.plant.LightningBambooSapling;
import ocean.chinacraft.common.world.block.plant.RicesEar;
import ocean.chinacraft.common.world.block.properties.PropertiesMisc;
import ocean.chinacraft.common.world.block.tree.MulberryTree;
import ocean.chinacraft.common.config.GroupDefine;
import ocean.chinacraft.common.constants.Constants;

import java.util.function.Supplier;

/**
 * 方块加载
 *
 * @author zhang
 * @since 2021/10/30
 */
public class ModBlocks {

    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS,
            Constants.MOD_ID);

    /**
     * 铜矿石
     */
    public static final RegistryObject<Block> COPPER_ORE = registryAll("copper_ore",
            () -> new Block(PropertiesMisc.defaultStone().harvestLevel(2)),
            GroupDefine.CHINACRAFT_MISC);

    /**
     * 锡矿
     */
    public static final RegistryObject<Block> TIN_ORE = registryAll("tin_ore",
            () -> new Block(PropertiesMisc.defaultStone().harvestLevel(2)),
            GroupDefine.CHINACRAFT_MISC);

    /**
     * 银矿
     */
    public static final RegistryObject<Block> SILVER_ORE = registryAll("silver_ore",
            () -> new Block(PropertiesMisc.defaultStone().harvestLevel(2)),
            GroupDefine.CHINACRAFT_MISC);

    /**
     * 玉原石
     */
    public static final RegistryObject<Block> JADE_ORE = registryAll("jade_ore",
            () -> new Block(PropertiesMisc.defaultStone().harvestLevel(2).lightLevel(value -> 3)),
            GroupDefine.CHINACRAFT_MISC);

    /**
     * 大理石
     */
    public static final RegistryObject<Block> MARBLE = registryAll("marble",
            () -> new Block(PropertiesMisc.defaultStone().harvestLevel(1)),
            GroupDefine.CHINACRAFT_MISC);

    /**
     * 灶台
     */
    public static final RegistryObject<Block> FURNACE_PLATFORM_BLOCK = registryAll("furnace_platform_block",
            FurnacePlatformBlock::new, GroupDefine.CHINACRAFT_FUNCTION);

    /**
     * 玉石加工台
     */
    public static final RegistryObject<Block> JADE_WORKBENCH_BLOCK = registryAll("jade_workbench_block",
            JadeWorkbenchBlock::new, GroupDefine.CHINACRAFT_FUNCTION);

    /**
     * 养蚕架
     */
    public static final RegistryObject<Block> SERICULTURE_FRAME_BLOCK = registryAll("sericulture_frame_block",
            SericultureFrameBlock::new, GroupDefine.CHINACRAFT_FUNCTION);

    /**
     * 米穗
     */
    public static final RegistryObject<Block> RICES_EAR = registryAll("rices_ear", RicesEar::new,
            GroupDefine.CHINACRAFT_AGRICULTURE);

    /**
     * 桑树树叶
     */
    public static final RegistryObject<Block> MULBERRY_LEAVES = registryAll("mulberry_leaves",
            () -> new LeavesBlock(AbstractBlock.Properties.copy(Blocks.OAK_LEAVES)),
            GroupDefine.CHINACRAFT_AGRICULTURE);

    /**
     * 桑树树苗
     */
    public static final RegistryObject<Block> MULBERRY_SAPLING = registryAll("mulberry_sapling",
            () -> new SaplingBlock(new MulberryTree(), AbstractBlock.Properties.copy(Blocks.OAK_SAPLING))
            , GroupDefine.CHINACRAFT_AGRICULTURE);

    /**
     * 桑木木头
     */
    public static final RegistryObject<Block> MULBERRY_LOG = registryBlock("mulberry_log",
            () -> new RotatedPillarBlock(AbstractBlock.Properties.copy(Blocks.OAK_LOG)));

    /**
     * 桑树木板
     */
    public static final RegistryObject<Block> MULBERRY_PLANK = registryBlock("mulberry_planks",
            () -> new Block(AbstractBlock.Properties.copy(Blocks.OAK_PLANKS)));

    /**
     * 映山红
     */
    public static final RegistryObject<Block> AZALEA = registryAll("azalea", () -> new FlowerBlock(Effects.DIG_SPEED,
                    2, AbstractBlock.Properties.copy(Blocks.DANDELION)),
            GroupDefine.CHINACRAFT_AGRICULTURE);

    /**
     * 牡丹
     */
    public static final RegistryObject<Block> PEONY = registryAll("peony", () -> new FlowerBlock(Effects.DIG_SPEED, 2
                    , AbstractBlock.Properties.copy(Blocks.DANDELION)),
            GroupDefine.CHINACRAFT_AGRICULTURE);

    /**
     * 菊花
     */
    public static final RegistryObject<Block> CHRYSANTHEMUM = registryAll("chrysanthemum",
            () -> new FlowerBlock(Effects.DIG_SPEED, 2, AbstractBlock.Properties.copy(Blocks.DANDELION)),
            GroupDefine.CHINACRAFT_AGRICULTURE);

    /**
     * 平滑大理石
     */
    public static final RegistryObject<Block> SMOOTH_MARBLE = registryAll("smooth_marble",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 条纹大理石
     */
    public static final RegistryObject<Block> MARBLE_PILLAR = registryAll("pillar_marble",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 錾制大理石
     */
    public static final RegistryObject<Block> CHISELED_MARBLE = registryAll("chiseled_marble",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 大理石半砖
     */
    public static final RegistryObject<Block> SLAB_MARBLE = registryAll("slab_marble",
            () -> new SlabBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 大理石楼梯
     */
    public static final RegistryObject<Block> STAIR_MARBLE = registryAll("stair_marble",
            () -> new StairsBlock(() -> MARBLE.get().defaultBlockState(), PropertiesMisc.defaultDecoration()),
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 木窗格
     */
    public static final RegistryObject<Block> WOOD_WINDOW = registryAll("wood_window", Window::new,
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 木窗格 样式2
     */
    public static final RegistryObject<Block> WOOD_WINDOW2 = registryAll("wood_window2", Window::new,
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 木窗格 样式3
     */
    public static final RegistryObject<Block> WOOD_WINDOW3 = registryAll("wood_window3", Window::new,
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 木窗格 样式4
     */
    public static final RegistryObject<Block> WOOD_WINDOW4 = registryAll("wood_window4", Window::new,
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 木窗格 龙
     */
    public static final RegistryObject<Block> WOOD_WINDOW_DRAGON = registryAll("wood_window_dragon", Window::new,
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 木窗格 福
     */
    public static final RegistryObject<Block> WOOD_WINDOW_FU = registryAll("wood_window_fu", Window::new,
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 青砖块
     */
    public static final RegistryObject<Block> BRICK_BLACK = registryAll("brick_black",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 青砖楼梯
     */
    public static final RegistryObject<Block> STAIR_BRICK_BLACK = registryAll("stair_brick_black",
            () -> new StairsBlock(() -> BRICK_BLACK.get().defaultBlockState(), PropertiesMisc.defaultDecoration()),
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 青砖半砖
     */
    public static final RegistryObject<Block> SLAB_BRICK_BLACK = registryAll("slab_brick_black",
            () -> new SlabBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 元宵灯笼
     */
    public static final RegistryObject<Block> LANTERN_FESTIVAL_LANTERNS = registryAll("lantern_festival_lanterns",
            LanternFestivalLanterns::new,
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹板
     */
    public static final RegistryObject<Block> BAMBOO_PLANK = registryAll("bamboo_plank",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹楼梯
     */
    public static final RegistryObject<Block> STAIR_BAMBOO = registryAll("stair_bamboo",
            () -> new StairsBlock(() -> BAMBOO_PLANK.get().defaultBlockState(), PropertiesMisc.defaultDecoration()),
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹台阶
     */
    public static final RegistryObject<Block> SLAB_BAMBOO = registryAll("slab_bamboo",
            () -> new SlabBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹栅栏
     */
    public static final RegistryObject<Block> BAMBOO_FENCE = registryAll("bamboo_fence",
            () -> new FenceBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹栅栏 门
     */
    public static final RegistryObject<Block> BAMBOO_FENCE_GATE = registryAll("bamboo_fence_gate",
            () -> new FenceGateBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 耐火砖块
     */
    public static final RegistryObject<Block> FIRE_BRICK_BLOCK = registryAll("fire_brick_block",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 已杀青的竹板
     */
    public static final RegistryObject<Block> PEELED_BAMBOO_PLANK = registryAll("peeled_bamboo_plank",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹窗格
     */
    public static final RegistryObject<Block> PEELED_BAMBOO_WINDOW = registryAll("peeled_bamboo_window",
            Window::new, GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹窗格 样式2
     */
    public static final RegistryObject<Block> PEELED_BAMBOO_WINDOW2 = registryAll("peeled_bamboo_window2",
            Window::new, GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 竹窗格 样式3
     */
    public static final RegistryObject<Block> PEELED_BAMBOO_WINDOW3 = registryAll("peeled_bamboo_window3",
            Window::new, GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 苔石青砖
     */
    public static final RegistryObject<Block> MOSSY_BRICK_BLACK = registryAll("mossy_brick_black",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 深色青砖块
     */
    public static final RegistryObject<Block> BLACKER_BRICK_BLACK = registryAll("blacker_brick_black",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 大理石墙
     */
    public static final RegistryObject<Block> MARBLE_WALL = registryAll("marble_wall",
            () -> new WallBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 青砖墙
     */
    public static final RegistryObject<Block> BRICK_BLACK_WALL = registryAll("brick_black_wall",
            () -> new WallBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 已杀青的竹制楼梯
     */
    public static final RegistryObject<Block> STAIR_PEELED_BAMBOO = registryAll("stair_peeled_bamboo",
            () -> new StairsBlock(() -> PEELED_BAMBOO_PLANK.get().defaultBlockState(),
                    PropertiesMisc.defaultDecoration()),
            GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 已杀青的竹台阶
     */
    public static final RegistryObject<Block> SLAB_PEELED_BAMBOO = registryAll("slab_peeled_bamboo",
            () -> new SlabBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 已杀青的竹栅栏
     */
    public static final RegistryObject<Block> PEELED_BAMBOO_FENCE = registryAll("peeled_bamboo_fence",
            () -> new FenceBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 已杀青的竹栅栏 门
     */
    public static final RegistryObject<Block> PEELED_BAMBOO_FENCE_GATE = registryAll("peeled_bamboo_fence_gate",
            () -> new FenceGateBlock(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_DECORATE);

    /**
     * 铜块
     */
    public static final RegistryObject<Block> COPPER_BLOCK = registryAll("copper_block",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_MISC);

    /**
     * 青铜块
     */
    public static final RegistryObject<Block> BRONZE_BLOCK = registryAll("bronze_block",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_MISC);

    /**
     * 锡块
     */
    public static final RegistryObject<Block> TIN_BLOCK = registryAll("tin_block",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_MISC);

    /**
     * 银块
     */
    public static final RegistryObject<Block> SILVER_BLOCK = registryAll("silver_block",
            () -> new Block(PropertiesMisc.defaultDecoration()), GroupDefine.CHINACRAFT_MISC);

    /**
     * 雷笋
     */
    public static final RegistryObject<Block> LIGHTNING_BAMBOO_SAPLING = registryAll("lightning_bamboo_sapling",
            LightningBambooSapling::new, GroupDefine.CHINACRAFT_FOOD);


    private static <T extends Block> RegistryObject<T> registryAll(String name, Supplier<T> block, ItemGroup group) {
        RegistryObject<T> register = BLOCKS.register(name, block);
        registerBlockItem(name, register, group);
        return register;
    }

    private static <T extends Block> RegistryObject<T> registryBlock(String name, Supplier<T> block) {
        return BLOCKS.register(name, block);
    }

    private static <T extends Block> void registerBlockItem(String name, RegistryObject<T> block, ItemGroup group) {
        ModItems.ITEMS.register(name, () -> new BlockItem(block.get(), new Item.Properties().tab(group)));
    }

    public static void register(IEventBus bus) {
        BLOCKS.register(bus);
    }
}
